<?php
/**
 * Created by PhpStorm.
 * User: pc
 * Date: 2019/1/14
 * Time: 20:18
 */

namespace app\api\controller;

use think\Db;

class Workorder extends Main
{
    public $uid;
    public $role_id;
    public $username;
    public $truename;
    public $password;
    public $company_id;
    public $type;
    public $officeManage = 3;//1总公司总管理员2子公司管理员3员工


    /**
     * 用户验证
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function verifyLogin()
    {
        $this->username = get('username');
        $this->password = get('password');
        $res = $this->detectLogin($this->username,$this->password);
        if($res['state'] == false){
            $this->returnJson($res['content']);
        }
        $user = $res['content'];
        $this->uid = $user['id'];
        $this->company_id = $user['company_id'];
        $this->type = $user['type'];
        $this->truename = $user['truename'];
        $this->role_id = $user['role_id'];
        //总公司总管理员
        if (($user['super'] == 2) && ($user['pid'] == 1) && ($user['company_id'] == 0) && ($user['type'] == 1)) {
            $this->officeManage = 1;
        }
        //子管理员
        if (($user['super'] == 3) && ($user['type'] == 2)) {
            $this->officeManage = 2;
        }
    }

    /**
     * 弹窗
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function popup()
    {
        //用户登录验证
        $this->verifyLogin();

        $state = false;
        $content = [];
        $clickid = '';
        //获取申请工单接收消息
        $where['user_id'] = $this->uid;
        $where['company_id'] = $this->company_id;
        $where['send_popup'] = 1;
        $send_popup = Db::name('work_order')->where($where)->count('id');
        if ($send_popup > 0) {
            $content[] = $send_popup . "条工单已有回复";
            $clickid = 'popmyworkorder();';//打开我的工单页面
            $state = true;
        }

        //查找工单url 的rule_id
        $rwhere['name'] = 'admin/workorder/workOrderDisposeList';
        $rwhere['type'] = 1;
        $rwhere['flag'] = 1;
        $rule = Db::name('auth_rule')->where($rwhere)->find();
        if (!checkArr($rule)) {//不存在菜单
            $this->returnWorkJson($content, $clickid, $state);
        }


        $pwhere['role_id'] = $this->role_id;
        $pwhere['rule_id'] = $rule['id'];
        $role = Db::name('auth_role_power')->where($pwhere)->find();
        if (!checkArr($role)) {//没有权限
            $this->returnWorkJson($content, $clickid, $state);
        }

        $where = [];
        $where1 = '';
        if ($this->type == 2) {//子公司
            $where['company_id'] = $this->company_id;
            $where['commit'] = 1;
        } else {
            $where1 = "company_id = " . $this->company_id . " OR commit=2";
        }
        $where['receive_popup'] = 1;
        $where['status'] = ['neq', 3];
        $where['user_id'] = ['neq', $this->uid];

        $receive_popup = Db::name('work_order')->where($where)->where($where1)->count('id');
        if ($receive_popup > 0) {
            $content[] = $receive_popup . "条工单需要处理";
            $clickid = !empty($clickid) ? 'popworkorder();' : 'popdisposeworkorder();';
            $state = true;
        }
        $this->returnWorkJson($content, $clickid, $state);
    }

    /**
     * 返回json
     * @param array $title
     * @param array $content
     * @param string $clickid
     * @param bool $state
     */
    private function returnWorkJson($content = [], $clickid = '', $state = false)
    {
        $arr = array(
            'hasmsg' => $state,
            'title' => "你有工单待处理",
            'content' => checkArr($content) ? "你有" . join(',', $content) : '',
            'clickid' => $clickid
        );
        echo json_encode($arr);
        exit;
    }


}